/*
 * @Author: shengduiliang 1107722390@qq.com
 * @Date: 2024-05-05 16:14:17
 * @LastEditors: shengduiliang 1107722390@qq.com
 * @LastEditTime: 2024-05-05 17:03:00
 * @FilePath: /treasure/src/utils/image.ts
 * @Description: 选择图片
 */
import { uploadFile } from "@/network/upload"
import Taro from "@tarojs/taro"
import to from "await-to-js"

const chooseImages = async (count: number): Promise<string[]> => {
  const [err, res] = await to(Taro.chooseImage({
    count,
    sizeType: ['original', 'compressed'],
    sourceType: ['album', 'camera'],
  }))
  if (err) return []
  return res?.tempFilePaths || []
}

const uploadImages = async (images: string[]): Promise<string[]> => { // Updated return type to string[]
  Taro.showLoading({ title: '图片上传中' })
  const [err, imageUrls] =  await to(Promise.all(images.map(image => uploadFile(image))))
  Taro.hideLoading()
  if (err) return [] // Updated return value to []
  return imageUrls
}

const getCdnImage = (url: string): string => {
  return `${process.env.TARO_APP_CDN_URL}${url}`
}

export default {
  chooseImages,
  uploadImages,
  getCdnImage
}


